/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity;

import static android.text.Spanned.SPAN_EXCLUSIVE_INCLUSIVE;
import static com.dataoke.shoppingguide.app773026.bean.Constants.SPLASH_TIME;
import static com.dataoke.shoppingguide.app773026.bean.Constants.SP_PRIVACY;
import static com.dataoke.shoppingguide.app773026.bean.Constants.SP_VERSION_CODE;
import static com.dataoke.shoppingguide.app773026.bean.Constants.STOP_SPLASH;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.JKJSharedPreference;
import com.dataoke.shoppingguide.app773026.util.AppUtil;
/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class PrivacyManage {
    private static long versionCode;
    private static long currentVersionCode;

    private static boolean isCheckPrivacy = false;

    public static boolean applyPrivacy() {
       return ! (!isCheckPrivacy || versionCode != currentVersionCode);
    }
    /**
     * 检查是否显示隐私政策
     */
    public static void checkShow(Activity activity, Handler handler) {

        //获取app当前版本号
        currentVersionCode = AppUtil.getAppVersionCode(activity);
        //获取app保存的版本号
        versionCode = JKJSharedPreference.getValue(SP_VERSION_CODE, 0L);
        //获取是否显示过隐私策略
        isCheckPrivacy = JKJSharedPreference.getValue(SP_PRIVACY, false);

        if (!isCheckPrivacy || versionCode != currentVersionCode) {
            //显示隐私策略
            show(activity, handler);
        } else {
            closeSplash(handler);
        }
    }

    /**
     * 显示用户协议和隐私政策
     */
    public static void show(Activity activity, Handler handler) {

        final PrivacyDialog dialog = new PrivacyDialog(activity);
        TextView tv_privacy_tips = dialog.findViewById(R.id.tv_privacy_tips);
        TextView tv_privacy_tips2 = dialog.findViewById(R.id.tv_privacy_tips2);
        tv_privacy_tips2.setText(R.string.privacy_tips2);
        TextView btn_exit = dialog.findViewById(R.id.btn_exit);
        TextView btn_enter = dialog.findViewById(R.id.btn_enter);
        dialog.show();

        String string = activity.getResources().getString(R.string.privacy_tips);
        String key1 = activity.getResources().getString(R.string.privacy_tips_key1);
        String key2 = activity.getResources().getString(R.string.privacy_tips_key2);
        int index1 = string.indexOf(key1);
        int index2 = string.indexOf(key2);

        //需要显示的字串
        SpannableString spannedString = new SpannableString(string);
        //设置点击字体颜色
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(activity.getResources().getColor(R.color.red));
        spannedString.setSpan(colorSpan1, index1, index1 + key1.length(), SPAN_EXCLUSIVE_INCLUSIVE);
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(activity.getResources().getColor(R.color.red));
        spannedString.setSpan(colorSpan2, index2, index2 + key2.length(), SPAN_EXCLUSIVE_INCLUSIVE);
        //设置点击字体大小
        AbsoluteSizeSpan sizeSpan1 = new AbsoluteSizeSpan(16, true);
        spannedString.setSpan(sizeSpan1, index1, index1 + key1.length(), SPAN_EXCLUSIVE_INCLUSIVE);
        AbsoluteSizeSpan sizeSpan2 = new AbsoluteSizeSpan(16, true);
        spannedString.setSpan(sizeSpan2, index2, index2 + key2.length(), SPAN_EXCLUSIVE_INCLUSIVE);
        //设置点击事件
        ClickableSpan clickableSpan1 = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(activity, TermsActivity.class);
                activity.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                //点击事件去掉下划线
                ds.setUnderlineText(false);
            }
        };
        spannedString.setSpan(clickableSpan1, index1, index1 + key1.length(), SPAN_EXCLUSIVE_INCLUSIVE);

        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(activity, PrivacyPolicyActivity.class);
                activity.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                //点击事件去掉下划线
                ds.setUnderlineText(false);
            }
        };
        spannedString.setSpan(clickableSpan2, index2, index2 + key2.length(), SPAN_EXCLUSIVE_INCLUSIVE);

        //设置点击后的颜色为透明，否则会一直出现高亮
        tv_privacy_tips.setHighlightColor(Color.TRANSPARENT);
        //开始响应点击事件
        tv_privacy_tips.setMovementMethod(LinkMovementMethod.getInstance());

        tv_privacy_tips.setText(spannedString);

        //设置弹框宽度占屏幕的80%
        WindowManager m = activity.getWindowManager();
        Display defaultDisplay = m.getDefaultDisplay();
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (int) (defaultDisplay.getWidth() * 0.80);
        dialog.getWindow().setAttributes(params);

        btn_exit.setOnClickListener(v -> {
            dialog.dismiss();
            JKJSharedPreference.save(SP_VERSION_CODE, currentVersionCode);
            JKJSharedPreference.save(SP_PRIVACY, false);
            activity.finish();
        });

        btn_enter.setOnClickListener(v -> {
            dialog.dismiss();
            versionCode = currentVersionCode;
            JKJSharedPreference.save(SP_VERSION_CODE, versionCode);
            isCheckPrivacy = true;
            JKJSharedPreference.save(SP_PRIVACY, isCheckPrivacy);
            //用户同意i隐私政策后关闭
            closeSplash(handler);
        });

    }


    public static void closeSplash(Handler splashHandler) {
        Message msg = new Message();
        msg.what = STOP_SPLASH;
        splashHandler.sendMessageDelayed(msg, SPLASH_TIME);
    }
}
